﻿using System;
using System.Collections;
using System.Reflection;

namespace ZilLion.Service.Core.ConfigManager.Helper
{
    public static class ConfigHelper
    {
        private static readonly Hashtable SMethodDict = Hashtable.Synchronized(new Hashtable(10240));
        public static T GetCustomAttribute<T>(object source) where T : System.Attribute
        {
            var type = source.GetType();

            return (T)(type.IsEnum ?
                System.Attribute.GetCustomAttribute(type.GetField(source.ToString()), typeof(T)) :
                System.Attribute.GetCustomAttribute(type, typeof(T)));
        }

        public static object FastNew(Type instanceType)
        {
            if (instanceType == null)
                throw new ArgumentNullException("instanceType");

            CtorDelegate ctor = (CtorDelegate)SMethodDict[instanceType];
            if (ctor == null)
            {
                ConstructorInfo ctorInfo = instanceType.GetConstructor(Type.EmptyTypes);
                ctor = DynamicMethodFactory.CreateConstructor(ctorInfo);
                SMethodDict[instanceType] = ctor;
            }

            return ctor();
        }

    }

    
}